/*请求路径47.117.135.38 localhost*/
serverPath = "http://47.117.135.38:8181";

if (getToken() === "") {
    setToken("000000");
    window.location.href = "login.html";
}

//退出
$(document).on("click", "#loginOut", function () {
    removeToken();
    window.location.href = "login.html";
});

// 当有多个参数的时候的取值 例如 ?param=1&param=2&param=3
var paramsSerializer = function (params) {
    return jQuery.param(params);
};

// 通过localStorage技术设置存储在浏览器中的token值
function setToken(str) {
    return localStorage.setItem("token", str);
}

// 获取通过localStorage技术存储在浏览器中的token值
function getToken() {
    var token = localStorage.getItem("token");
    return token || "";
}

// 获取通过localStorage技术存储在浏览器中的token值
function removeToken() {
    localStorage.removeItem()("token");
}

var Base64 = {
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    encode: function (e) {
        var t = "";
        var n, r, i, s, o, u, a;
        var f = 0;
        e = Base64._utf8_encode(e);
        while (f < e.length) {
            n = e.charCodeAt(f++);
            r = e.charCodeAt(f++);
            i = e.charCodeAt(f++);
            s = n >> 2;
            o = (n & 3) << 4 | r >> 4;
            u = (r & 15) << 2 | i >> 6;
            a = i & 63;
            if (isNaN(r)) {
                u = a = 64
            } else if (isNaN(i)) {
                a = 64
            }
            t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o)
                + this._keyStr.charAt(u) + this._keyStr.charAt(a)
        }
        return t
    },
    decode: function (e) {
        var t = "";
        var n, r, i;
        var s, o, u, a;
        var f = 0;
        e = e.replace(/[^A-Za-z0-9+/=]/g, "");
        while (f < e.length) {
            s = this._keyStr.indexOf(e.charAt(f++));
            o = this._keyStr.indexOf(e.charAt(f++));
            u = this._keyStr.indexOf(e.charAt(f++));
            a = this._keyStr.indexOf(e.charAt(f++));
            n = s << 2 | o >> 4;
            r = (o & 15) << 4 | u >> 2;
            i = (u & 3) << 6 | a;
            t = t + String.fromCharCode(n);
            if (u != 64) {
                t = t + String.fromCharCode(r)
            }
            if (a != 64) {
                t = t + String.fromCharCode(i)
            }
        }
        t = Base64._utf8_decode(t);
        return t
    },
    _utf8_encode: function (e) {
        e = e.replace(/rn/g, "n");
        var t = "";
        for (var n = 0; n < e.length; n++) {
            var r = e.charCodeAt(n);
            if (r < 128) {
                t += String.fromCharCode(r)
            } else if (r > 127 && r < 2048) {
                t += String.fromCharCode(r >> 6 | 192);
                t += String.fromCharCode(r & 63 | 128)
            } else {
                t += String.fromCharCode(r >> 12 | 224);
                t += String.fromCharCode(r >> 6 & 63 | 128);
                t += String.fromCharCode(r & 63 | 128)
            }
        }
        return t
    },
    _utf8_decode: function (e) {
        var t = "";
        var n = 0;
        var r = c1 = c2 = 0;
        while (n < e.length) {
            r = e.charCodeAt(n);
            if (r < 128) {
                t += String.fromCharCode(r);
                n++
            } else if (r > 191 && r < 224) {
                c2 = e.charCodeAt(n + 1);
                t += String.fromCharCode((r & 31) << 6 | c2 & 63);
                n += 2
            } else {
                c2 = e.charCodeAt(n + 1);
                c3 = e.charCodeAt(n + 2);
                t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3
                    & 63);
                n += 3
            }
        }
        return t;
    }
}


/**
 * 获取前端请求参数的方法
 * 将参数名和参数值保存为JSON对象格式
 */
function getParams() {
    var paramStrs = location.search.replace("?", "").split("&");
    //对数组中的每个元素根据等号进行拆分，拆分得到的数组第一元素就是参数名，第二个元素就是参数值
    //将最后获取到的参数信息保存为一个对象，key就是参数名，value就是参数值
    var params = {};
    for (var i = 0; i < paramStrs.length; i++) {
        var paramStr = paramStrs[i];
        var nameValuePair = paramStr.split("=");
        params[nameValuePair[0]] = decodeURI(nameValuePair[1]);
    }
    return params;
}

/**
 * 该方法用于删除layui数据表格中
 * 复选框CheckBox 中的单选或者多选的删除操作
 */
function deleteAny(anyNoList, url, tableNode) {
    var anyNos = (typeof anyNoList == 'object') ? anyNoList : [anyNoList];
    layer.confirm("确定要删除吗？", function (index) {
            axios.delete(url, {
                params: {
                    ids: anyNos
                },
                paramsSerializer: function (params) {
                    return Qs.stringify(params, {
                        arrayFormat: 'repeat'
                    });
                },
                headers: {
                    "auth-token": getToken()
                }
            }).then(function (response) {
                layer.close(index);
                //刷新表格
                // table.reload(tableNode);
                if (response.status == 204) {
                    layer.confirm("删除成功...", function () {
                        layer.closeAll();
                    })
                }
            }).catch(function (error) {
                if (error.response) {
                    // 请求已发出，但服务器响应的状态码不在 2xx 范围内
                    console.log(error.response.data);
                } else {
                    // Something happened in setting up the request that triggered an Error
                    layer.alert(error.message);
                }
            });
        }
    );
}

/**
 * ueditor
 */
//设置编辑器初始化内容
function setUeValue(Ueditor, value) {

    Ueditor.ready(function () {

        //默认显示内容
        Ueditor.setContent(value);
        //文本框获取焦点时清空默认显示的内容
        /* editor.addListener("focus", function() {
         editor.setContent('');
        }); */
        //文本框是去焦点时,若内容为空则显示默认显示的内容
        Ueditor.addListener("blur", function () {
            if (!Ueditor.getContent()) {
                Ueditor.setContent(value);
            }
        });
    });

}

/**
 * 删除ueditor
 */
function emptyUE(Ueditor) {
    Ueditor.ready(function () {
        Ueditor.setContent('');
    });
}

/**
 * 判断跳转手机还是电脑页面
 */
function locationHref() {
    ////屏幕可用工作区宽
    const clientWidth = window.screen.availWidth;
    if (clientWidth >= 992) {
        // layer.msg('请通过手机渠道进行访问！', {icon: 2});
        $(".layui-form").css("margin-left", "30%");
        //电脑后台主页
        location.href = 'main.html';
    } else {
        //手机后台主页
        location.href = 'phone-main.html';
    }
}